petitcomputerfandomcom-20200214-history
Multiplayer and how it works/is possible
Yes, yes I know I have not been on in forever, but I decided to get a little involved into the community so it does not seem like a surpise when I release info of my newest project. Anyways onto the tutorial. How to make a multiplayer game I find that it is easiest to teach things by breaking them down into smaller more portable chunks. So what is online/wireless multiplayer. Well simply put it is 2 or more people playing a game on seperate devices in a way that they can interact and affect each other with the use of packets(little bits of information) to communicate to each other what they are doing. Now before you try to port Doom you need to know something. Petit computer is invariably limited and as such real time games would be broken from waiting for player to send and recieve these packets. There are 2 commands involved in this process. SENDFILE and RECFILE work by openening a dialogue asking the user who they want to receive/send a file to. This stops everything else that is happening until it is complete. This is what I mean by limited. So let's figure out what is necessary for a game, in this case chess(I will post my demo soon I just need to create all the pieces!). For the person taking their turn, you have to get the location of the piece they want to move and check if there is actually a piece, get the location they want to move too and check if it is possible and then send the data. So all you need are 2 (x,y)coordinates. In addition to this packet you should create a special one for when a player wants to quit for instance it could look like this "I AM A LOSER". I will go through this step by step and then have some tips at the end. #Get selected location that the player wants to move from. Is there a piece there? #If there was a piece then save the coordinates to a variable(s) for later use, Else go back to step 1. #Get coordinate they want to move to. Is it possible(e.g. a pawn can't move forward if there is a piece)? #If it is possible to move to the new location then save those coordinates to a varialbe(s). Else go back to step 3. #Set Mem$ to empty "". #Put the coordinates into Mem$ with str$(). Mem$ = str$(coords1) + str$(coords2)(e.g. Mem$ = "3286") #Send packet to waiting other player(This has to be done manually by user). #The application of the player that was waiting checks if the move was possible(cheating/error in code). #Repeat, alternating with each player. And this is genrally the same for any game. You get input check if it works and then send to the other players who then check it. This type of connection of connection is called Peer-to-Peer(P2P) where all of the players applications have a say in what happens instead of Peer-to_Server(P2S) where most/all of the input is validated by a server. For example Doom uses this type of multiplayer connection(besides the fact that there was no peer validation). Tips & Tricks Bit Packing: : This is a standard 16 bit(4 byte) integer : Last one is sign bit........... 128 64 32 16 8 4 2 1 : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 : You might be thinking that 4 bits(1 nibble = 1/2 byte) is just enough to represent a single x or y coordinate(in chess). While this does not save much space for you could put several states(16) into a single variable possibly saving you characters in Mem$ in a more complex game. Peer packet validation: : I mentioned this above, but I will put it here just because it is not completly necessary. After a packet is sent from player(1/2) the other player(1 or 2) will check to make sure that what player(1/2) did was possible. In laymans terms "(CoD) U CANT FLY ***!!?!! OMG HAXORSJKGK!". Random value: : Let's take pokemon for example. If you take a random number from 1-100 and you have to get 95 or more for a critical what are the chances. Well 5(100-95) divided by 100 is 0.05 or 1/20. So for every 20 moves there will be roughly 1 critical hit. So to ensure there is no cheating or an error making things work incorrectly you can set a maximum number of times that a critical can happen(either in a row or a certain number of moves). If player1 reaches this limit then player2 will request that player1 send another value. If it happens again then player2 will generate a random value and send that to player1. If you don't want to code this logic than you can just notify player2 that there is something wrong with player1's game. Chess Game(Coming Soon) Category:Tutorials